<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建积分规则':'编辑积分规则'" :visible.sync="visible" :append-to-body="appendToBody" :before-close="onClose">
    <el-form ref="form" :model="form" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item label="业务类别" prop="businessType">
          <el-input v-model="form.businessType" readonly />
        </el-form-item>
        <el-form-item label="名称" prop="businessName">
          <el-input v-model="form.businessName" readonly />
        </el-form-item>
        <el-form-item label="成长积分" prop="growBounds">
          <el-input-number v-model="form.growBounds" :min="0" />
        </el-form-item>
        <el-form-item label="消费积分" prop="buyBounds">
          <el-input-number v-model="form.buyBounds" :min="0" />
        </el-form-item>
        <el-form-item label="优惠生效情况" prop="work">
          <el-input v-model="form.work" :min="1000" :max="9999" />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { boundsLoad, boundsSave } from '@/api/system/member'

export default {
  name: 'MemberBoundsDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        businessId: undefined,
        businessName: undefined,
        businessType: undefined,
        businessTypeName: undefined,
        growBounds: 0,
        buyBounds: 0,
        work: 1000
      }
    }
  },
  methods: {
    init(id, businessId, businessName, businessTypeName) {
      this.hasLoaded = false
      this.form.id = id || null
      this.form.businessId = businessId
      this.form.businessName = businessName
      this.form.businessTypeName = businessTypeName
      this.form.businessType = 'mall' === businessTypeName ? '电商' : '博客'
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (this.form.id) {
          this.loadForm(id)
        } else {
          this.hasLoaded = this.$perm.hasPermissionOr(['system:bounds:edit'])
        }
      })
    },
    loadForm(id) {
      boundsLoad({'id': id}).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['system:bounds:edit'])
      }).catch(() => {})
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          boundsSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.$refs.form.resetFields()
            this.visible = false
          }).catch(() => {})
        }
      })
    },
    onClose() {
      this.$refs.form.resetFields()
      this.visible = false
    }
  }
}
</script>
